package com.splic.product.service.impl;

import com.splic.product.domain.UserSubRecord;
import com.splic.product.service.ClearUserSubStatusService;
import com.splic.product.service.UserSubRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ==========================
 * 开发：singe
 * 创建时间：2022-03-27 22:08
 * 版本: 1.0
 * 描述：TODO
 * ==========================
 */
@Service
@Slf4j
public class ClearUserSubStatusServiceImpl implements ClearUserSubStatusService {

    @Autowired
    private UserSubRecordService userSubRecordServiceImpl;


    @Override
    public void clearUserSubStatus() {
        try {
            long now = System.currentTimeMillis();
            //偏移值
            Long offsetId = 0L;
            for (int i = 1; i < 100000; i++) {
                List<UserSubRecord> userSubRecordList = userSubRecordServiceImpl.queryUserSubListByGreaterId(offsetId, 100);
                if (CollectionUtils.isEmpty(userSubRecordList)) {
                    break;
                }
                offsetId = userSubRecordList.get(userSubRecordList.size() - 1).getId();
                for (UserSubRecord userSubRecord : userSubRecordList) {
                    log.info("用户id：{},服务id:{}", userSubRecord.getUserId(), userSubRecord.getGoodsTypeId());
                    if (userSubRecord.getEffictEndTime() == null ||
                            userSubRecord.getEffictEndTime().getTime() < now) {
                        log.info("{}用户ID:{},开始时间:{},过期时间：{}", userSubRecord.getUserId(), userSubRecord.getEffictStartTime(),userSubRecord.getEffictEndTime());
                        userSubRecordServiceImpl.clearUserSubStatusById(userSubRecord.getId());
                    }
                }
            }


        } catch (Exception e) {
            log.error("清理用户订阅状态过期时间异常", e);
        }
    }
}
